let { request, connection } = require("../db/mysql.js");
let moment = require("moment"); //格式化时间
// 分类列表
exports.CategoryList = async(req, res) => {
    // http://localhost:3000/api/category/list?page=1&size=10

    // let { page = 1, size = 5 } = req.query
    let page = req.query.page || 1;
    let size = req.query.size || 5;



    let firstSql = `SELECT * from hg_category WHERE pid=0 and is_show=1 limit ${(page - 1) * size},${size}`
    let result = await request(firstSql)

    // 总数
    let countSql = `SELECT COUNT(*) FROM hg_category WHERE is_show=1 and pid=0`
    let resultCount = await request(countSql)
    console.log(resultCount[0]['COUNT(*)']);
    let count = resultCount[0]['COUNT(*)']
    let pages = Math.ceil(count / size)
    for (var i = 0; i < result.length; i++) {
        let secondSql = `SELECT * from hg_category WHERE pid=${result[i].id} and is_show=1`
        let secondResult = await request(secondSql)
        result[i].children = secondResult
        for (var j = 0; j < secondResult.length; j++) {
            let thirdSql = `SELECT * from hg_category WHERE pid=${secondResult[j].id} and is_show=1`
            let thirdResult = await request(thirdSql)
            secondResult[j].children = thirdResult
        }
    }
    res.json({
        status: 200,
        msg: "成功",
        count: count,
        page: Number(page),
        size: Number(size),
        pages: pages,
        data: result
    })
};
// 添加分类的列表获取
exports.AddCategoryList = async(req, res) => {
    // http://localhost:3000/api/category/addlist

    // // let { page = 1, size = 5 } = req.query
    // let page = req.query.page || 1;
    // let size = req.query.size || 5;

    let firstSql = `SELECT id as value, title as label from hg_category WHERE pid=0 and is_show=1 `
    let result = await request(firstSql)
    for (var i = 0; i < result.length; i++) {
        let secondSql = `SELECT id as value, title as label from hg_category WHERE pid=${result[i].value} and is_show=1`
        let secondResult = await request(secondSql)
        result[i].children = secondResult
    }
    result.unshift({
        value: 0,
        label: "顶级分类",
    })
    res.json({
        status: 200,
        msg: "成功",
        data: result
    })
}

// 添加分类接口
exports.AddCategory = async(req, res) => {
    // req.body  {pid:"",title:"",is_show:"",img:"",create_time:""}
    let { pid, title, is_show, img } = req.body
    let nowTime = moment().format("YYYY-MM-DD HH:mm:ss")
        // 判断要添加的分类名称是否存在
    let sql = `SELECT * from hg_category WHERE title="${title}"`
    let resultTitle = await request(sql)
    if (resultTitle.length > 0) {
        // 如果条件成立说明分类名称已经存在
        return res.json({
            status: 1001,
            msg: "该分类已存在"
        })
    } else {
        let addSql = `INSERT INTO hg_category SET pid="${pid}",img="${img}",title="${title}",is_show=${is_show},create_time="${nowTime}"`
        let result = await request(addSql)
        res.json({
            status: 200,
            msg: "添加成功",
            data: result
        })
    }

}

// 编辑分类接口
exports.EditCategory = async(req, res) => {
        let { pid, title, is_show, img, id } = req.body
        let nowTime = moment().format("YYYY-MM-DD HH:mm:ss")
            // 判断要添加的分类名称是否存在
        let sql = `SELECT * from hg_category WHERE title="${title}"`
        let resultTitle = await request(sql)
        if (resultTitle.length > 0) {
            // 如果条件成立说明分类名称已经存在
            return res.json({
                status: 1001,
                msg: "该分类已存在"
            })
        } else {
            let updateSql = `UPDATE hg_category SET title="${title}",is_show="${is_show}",img="${img}",pid="${pid}" WHERE id=${id}`
            let result = await request(updateSql)
            res.json({
                status: 200,
                msg: "修改成功",
                data: result
            })
        }
    }
    // 删除分类接口
exports.DeleCategory = async(req, res) => {
    let id = req.query.id
    console.log(id);

    let sql = `SELECT * from hg_category WHERE pid=${id}`
    let result = await request(sql)
    if (result.length > 0) {

        res.json({
            status: 1001,
            msg: "不能直接删除该分类",
            result: result
        })
    } else {
        let deleSql = `DELETE FROM hg_category WHERE id=${id}`
        let resultDele = await request(deleSql)
        res.json({
            status: 200,
            msg: "删除成功",
            data: resultDele
        })
    }
}